package com.yuanluludev.lib_base.utils

import android.os.Parcelable
import com.tencent.mmkv.MMKV
import com.yuanluludev.lib_utils.util.GsonUtils

/**
 * yuanLulu
 *  用于存储少量数据到本地
 */
class MMKVUtils private constructor(private val mv: MMKV) {

    /**
     * 双重校验同步锁单例
     */
    companion object {
        @Volatile
        private var instance: MMKVUtils? = null

        @JvmStatic
        fun getInstance() =
            instance ?: synchronized(this) {
                instance ?: MMKVUtils(MMKV.defaultMMKV()).also {
                    instance = it
                }
            }
    }

    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     */
    fun encode(key: String, obj: Any) {
        when (obj) {
            is String -> {
                mv.encode(key, obj)
            }
            is Int -> {
                mv.encode(key, obj)
            }
            is Boolean -> {
                mv.encode(key, obj)
            }
            is Float -> {
                mv.encode(key, obj)
            }
            is Long -> {
                mv.encode(key, obj)
            }
            is Double -> {
                mv.encode(key, obj)
            }
            is ByteArray -> {
                mv.encode(key, obj)
            }
            is Parcelable -> {
                mv.encode(key, obj)
            }
            //以字符串的形式存储一个对象
            else -> {
                mv.encode(key, GsonUtils.toJson(obj))
            }
        }
    }

    fun encodeSet(key: String, sets: Set<String?>?) {
        mv.encode(key, sets)
    }

    fun encodeParcelable(key: String, obj: Parcelable?) {
        mv.encode(key, obj)
    }

    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     */
    fun decodeInt(key: String, defaultValue: Int = 0): Int {
        return mv.decodeInt(key, defaultValue)
    }

    fun decodeDouble(key: String, defaultValue: Double = 0.00): Double {
        return mv.decodeDouble(key, defaultValue)
    }

    fun decodeLong(key: String, defaultValue: Long = 0L): Long {
        return mv.decodeLong(key, defaultValue)
    }

    fun decodeBoolean(key: String, defaultValue: Boolean = false): Boolean {
        return mv.decodeBool(key, defaultValue)
    }

    fun decodeFloat(key: String, defaultValue: Float = 0f): Float {
        return mv.decodeFloat(key, defaultValue)
    }

    fun decodeBytes(key: String): ByteArray? {
        return mv.decodeBytes(key)
    }

    fun decodeString(key: String, defaultValue: String = ""): String {
        return mv.decodeString(key, defaultValue)
    }

    fun decodeStringSet(key: String): Set<String?>? {
        return mv.decodeStringSet(key, emptySet<String>())
    }

    fun <T : Parcelable> decodeParcelable(key: String, clz: Class<T>): T? {
        return mv.decodeParcelable(key, clz)
    }

    /**
     * 返回一个对象
     */
    fun <T> decodeObj(key: String, clz: Class<T>): T? {
        val json = decodeString(key)
        return GsonUtils.fromJson(json, clz)
    }

    /**
     * 移除某个key对
     */
    fun removeKey(key: String?) {
        mv.removeValueForKey(key)
    }

    /**
     * 清除所有key
     */
    fun clearAll() {
        mv.clearAll()
    }

}
